<?php

namespace app\index\controller;
// 该文件的位于application\index\controller文件夹
// 教师模型
use app\common\model\Teacher;

use think\Controller;
use think\Request;
use think\Url;

/**
 * 登录管理
 */
class LoginController extends Controller
{

    // 用户登录表单
    public function index()
    {
        // 显示登录表单
        return $this->fetch();
    }

    // 处理用户提交的登录数据
    public function login()
    {
        //接受post信息
        $postData = Request::instance()->post();

        // 验证用户名是否存在
        /*        $map = array('username' => $postData['username']);
                $Teacher = Teacher::get($map);

                //$Teacher要么是一个对象，要么是null
                if (!is_null($Teacher) && $Teacher->getData('password') === $postData['password']) {
                    // 用户名密码正确 ，将teacherId 存session
                    session('teacherId', $Teacher->getData('id'));
                    return $this->success('login success', url('Teacher/index'));
                } else {
                    //用户名不存在，跳转到登录界面
                    return $this->error('username not exist', url('index'));
                }*/
        //直接调用M层方法，进行登录
        if (Teacher::login($postData['username'], $postData['password'])) {
            return $this->success('login success', url('Teacher/index'));
        } else {
            return $this->error('username or password incorrent', url('index'));
        }


    }

    //注销
    public function logOut()
    {
        if (Teacher::logOut()) {
            return $this->success('logout success', url('index'));
        } else {
            return $this->error('logout error', url('index'));
        }
    }
}



